enum PLAY_STATE{
    ACTION_CHUPAI = 1,
    ACTION_MOPAI,
    ACTION_PENG,
    ACTION_GANG,
    ACTION_HU,
}



export class ReplayMgr extends cc.Component{

    /**上一个播放状态 */
    _lastAction: cc.RecordAction;

    /**播放状态集合 */
    _actionRecords: any[];

    /**当前播放状态的索引 */
    _currentIndex = 0;

    init(data: cc.roomInfo){
        this._actionRecords = data.action_records;
        if(this._actionRecords == null){
            this._actionRecords = [];
        }
        this._currentIndex = 0;
        this._lastAction = null;
    }

    /**清空录像播放数据 */
    clear(){
        this._lastAction = null;
        this._actionRecords = null;
        this._currentIndex = 0;
    }

    /**是否在播放录像 */
    isReplay(){
        return this._actionRecords != null;
    }

    /**获取下一个需要播放的信息 */
    getNextAction(){
        if(this._currentIndex >= this._actionRecords.length){
            return null;
        }

        const si = this._actionRecords[this._currentIndex++];
        const action = this._actionRecords[this._currentIndex++];
        const pai = this._actionRecords[this._currentIndex++];
        return {si, type: action, pai};
    }

    /**执行操作并返回此操作需要的时间 */
    takeAction(){
        const action = this.getNextAction();
        if(this._lastAction != null && this._lastAction.type == PLAY_STATE.ACTION_CHUPAI){
            if (action != null && action.type != PLAY_STATE.ACTION_PENG && action.type != PLAY_STATE.ACTION_GANG && action.type != PLAY_STATE.ACTION_HU){
                cc.vv.gameNetMgr.doGuo(this._lastAction.si, this._lastAction.pai);
            }
        }

        this._lastAction = action;

        if(action == null){
            return -1;
        }

        let actionTime = 0;
        switch(action.type){
            case PLAY_STATE.ACTION_CHUPAI:
                cc.vv.gameNetMgr.doChupai(action.si, action.pai);
                actionTime = 1.0;
                break;
            case PLAY_STATE.ACTION_MOPAI:
                cc.vv.gameNetMgr.doMopai(action.si, action.pai);
                cc.vv.gameNetMgr.doTurnChange(action.si);
                actionTime = 0.5;
                break;
            case PLAY_STATE.ACTION_PENG:
                cc.vv.gameNetMgr.doPeng(action.si, action.pai);
                cc.vv.gameNetMgr.doTurnChange(action.si);
                actionTime = 1.0;
                break;
            case PLAY_STATE.ACTION_GANG:
                cc.vv.gameNetMgr.dispatchEvent(cc.vv.game_event_map.HANGANG_NOTIFY, action.si);
                cc.vv.gameNetMgr.doGang(action.si, action.pai);
                cc.vv.gameNetMgr.doTurnChange(action.si);
                actionTime = 1.0;
                break;
            case PLAY_STATE.ACTION_CHUPAI:
                cc.vv.gameNetMgr.doHu({seatIndex: action.si, hupai: action.pai, isZiMo: false});
                actionTime = 1.5;
                break;
        }

        return actionTime;
    }

}